package top.hzy520.personalblog.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import top.hzy520.personalblog.po.Type;
import top.hzy520.personalblog.service.BlogService;
import top.hzy520.personalblog.service.TypeService;
import top.hzy520.personalblog.vo.BlogQuery;

import java.util.List;

@Controller
public class TypesController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @GetMapping("/types/{id}")
    public String type(@PathVariable Long id, @PageableDefault(sort = {"updateTime"},direction = Sort.Direction.DESC,size = 6) Pageable pageable, Model model){
        List<Type> types=typeService.listType(10000);
        if (id==-1){
            id=types.get(0).getId();
        }
        BlogQuery blogQuery=new BlogQuery();
        blogQuery.setTypeId(id);
        model.addAttribute("page",blogService.listBlog(pageable,blogQuery));
        model.addAttribute("types",types);
        model.addAttribute("activeTypeId",id);
        return "types";
    }
}
